#include <xp/net/inet.h>
#include <xp/bas/stdio.h>
#include <xp/bas/string.h>

int xp_main (int argc, xp_char_t* argv[])
{
	xp_char_t buf[100];
	xp_uint32_t ip, ip2;
	int prefix;

	if (argc != 2) {
		xp_printf (XP_TEXT("%s: netmask\n"), argv[0]);
		return -1;
	}	

	xp_printf (XP_TEXT("Be patient!! This may take a very long time\n"));
	ip = 0;
	while (1) {
		if (xp_inet4_nmtopx (ip, &prefix) == 0) {
			xp_inet4_iptostrx (ip, buf, xp_countof(buf));
			xp_printf (XP_TEXT("%d -> %s\n"), prefix, buf);
		}

		if (ip >= XP_TYPE_MAX(xp_uint32_t)) break;
		ip++;
	}

	if (xp_inet4_strxtoip (argv[1], xp_strlen(argv[1]), &ip) == -1) {
		xp_printf (XP_TEXT("strxtoip [%s] - not a valid address\n"), argv[1]);
		return -1;
	}

	if (xp_inet4_nmtopx (ip, &prefix) == -1) {
		xp_printf (XP_TEXT("cannot convert to prefix\n"));
		return -1;
	}

	if (xp_inet4_pxtonm (prefix, &ip2) == -1 ||ip != ip2) {
		xp_printf (XP_TEXT("cannot convert back to ip\n"));
		return -1;
	}

	xp_printf (XP_TEXT("prefix = %d\n"), prefix);
	return 0;
}
